/**
 * 
 */
package br.unifor.mia.cd.middleware.arcademis.marshalable;

import arcademis.MarshalException;
import arcademis.Marshalable;
import arcademis.Stream;

/**
 * Serialização de objetos que implementam a interface: {@link Marshalable}
 * @author landim
 *
 */
public class MarshallerInterfaceMechanism implements MarshallerMechanism {

	/**
	 * singleton
	 */
	private static MarshallerInterfaceMechanism instance;
	
	/**
	 * Construtor privado.
	 */
	private MarshallerInterfaceMechanism() {
	}
	
	/**
	 * Pegar instância única
	 * @return singleton object
	 */
	public static MarshallerInterfaceMechanism getInstance() {
		if (instance == null) {
			instance = new MarshallerInterfaceMechanism();
		}
		return instance;
	}	
	
	/**
	 * @see br.unifor.mia.cd.middleware.arcademis.marshalable.MarshallerMechanism#marshall(arcademis.Stream, java.lang.Object)
	 */
	public void marshall(Stream stream, Object bean) throws MarshalException {
		if(bean != null && bean instanceof Marshalable) {
			((Marshalable) bean).marshal(stream);
		}
	}

	/**
	 * @see br.unifor.mia.cd.middleware.arcademis.marshalable.MarshallerMechanism#unmarshall(arcademis.Stream, java.lang.Object)
	 */
	public Object unmarshall(Stream stream, Object bean) throws MarshalException {
		if(bean != null && bean instanceof Marshalable) {
			((Marshalable) bean).unmarshal(stream);
		}
		return bean;
	}

}
